<?php


namespace app\miniProgram\controller;


use app\home\severs\InventoryOrderSever;
use think\Exception;
use think\facade\Db;

class InventoryOrder extends Auth
{
    protected $table='inventory_order';

    /**
     * @return bool|\think\response\Json
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/11/8
     * 确认后直接生成单据并确认并盘点
     */
    public function onSure()
    {
        $validate=new \app\miniProgram\validate\Action();
        if (!$validate->scene('pandian')->check($this->param)){
            return $this->jump(0,$validate->getError());
        }
        Db::connect('company')->startTrans();
        try {
            $inData=[
                'inventoryTime'=>$this->param['inventoryTime']??date('Y-m-d'),
                'warehouse_id'=>$this->param['wId'],
                'inOrderNum'=>orderNum('PDH'),
                'status'=>$this->param['status']??'1',
                'create_time'=>time(),
                'update_time'=>time(),
            ];
            $id=Db::connect('company')->name($this->table)->insertGetId($inData);
            $mxData=[
                'material_dossier_id'=>$this->param['mdId'],
                'num'=>$this->param['num'],
                'backNum'=>$this->param['backNum'],
                'dKey'=>suiji('pd'),
//                        'incom_receipt_detail_id'=>$incom_receipt_detail_id[$k],
                'pdNum'=>'0',
                'inventory_id'=>$id,
                'create_time'=>time(),
                'update_time'=>time()
            ];
            Db::connect('company')->name('inventory_order_detail')->save($mxData);
            $sureData=InventoryOrderSever::onSure(['id'=>$id],$this->userInfo['id']);

            if ($sureData['code']!=0){
                throw new Exception($sureData['msg']);
            }
            $pankui=0;
            $panying=0;
            $mxList=Db::connect('company')->name('inventory_order_detail')
                ->alias('iod')
                ->join('material_kucun_view omi',"omi.mdId=iod.material_dossier_id and omi.wId= {$inData['warehouse_id']}",'left')
                ->where('iod.status','>','-1')
                ->where('iod.inventory_id',$id)
                ->field('iod.*,omi.mdBrand,omi.countNum as baseNum,omi.sendNum,omi.mdName,omi.locatorName,omi.unit,omi.specs,omi.bar_code,omi.specs')
                ->select()->each(function ($item,$index) use (&$pankui,&$panying){
                    $item['haveNum']=$item['baseNum']-$item['sendNum'];
                    if ($item['haveNum']>$item['num']&&$item['is_pd']==2){
                        $pankui+=1;
                    }
                    if ($item['haveNum']<$item['num']&&$item['is_pd']==2){
                        $panying+=1;
                    }
                    return $item;
                });
            if ($panying){
                $pyData=InventoryOrderSever::panying(['id'=>$id],$this->userInfo['id']);

                if ($pyData['code']!=0){
                    throw new Exception($pyData['msg']);
                }
            }
            if ($pankui){
                $pkData=InventoryOrderSever::pankui(['id'=>$id],$this->userInfo['id']);

                if ($pkData['code']!=0){
                    throw new Exception($pkData['msg']);
                }
            }
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return $this->jump(0,$e->getMessage());
        }
        return $this->jump(1,'操作成功');
}

    /**
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/11/8
     * 编辑详情
     */
    public function add()
    {
        $validate=new \app\miniProgram\validate\Action();
        if (!$validate->scene('editPd')->check($this->param)){
            return $this->jump(0,$validate->getError());
        }
        $orderStatus=Db::connect('company')->name($this->table)->where('id',$this->param['orderId'])->value('status');
        if ($orderStatus!=1){
            return $this->jump(0,'单据不可编辑');
        }
        $updata=[
            'num'=>$this->param['num'],
            'update_time'=>time(),
            'inUser'=>$this->userInfo['id'],
            'isDo'=>1
        ];
        $where[]=['inventory_id','=',$this->param['orderId']];
        $where[]=['id','=',$this->param['detailId']];
        try {
            Db::connect('company')->name('inventory_order_detail')->where($where)->update($updata);
            add_log('pd', $this->param['orderId'], $this->param,'盘点单',$this->userInfo['id']);
        }catch (\Exception $e){
            return $this->jump(0,'操作失败:'.$e->getMessage());
        }
        return $this->jump(1);
}
}